//****************************************************************************//
//* CI7 Libreria grafica                                                     *//
//*	ci7_sound.hpp              										 	     *//
//*                                                                          *//
//* ci7.hddisseny.es                                                         *//
//* hddisseny@ci7.hddisseny.es                                               *//
//*																			 *//
//*	Ultima revision - 27/04/2012    										 *//
//****************************************************************************//
// Solo usa un canal para las canciones y sonidos !! FALLO

#include "ci7.hpp"

/*  Clase::metodo( Parametros )

    Explicacion
*/

void CI7_Sound::SoundInit(int mode ) {

    switch ( mode ) {
    case CI7_SOUND_LOW:
        _ci7Init_audio_rate = 22050;
        _ci7Init_audio_format = AUDIO_U8 ;
        _ci7Init_audio_channels = 1;
        _ci7Init_audio_buffers = 4096;
        break;
    case CI7_SOUND_MED:
        _ci7Init_audio_rate = 32000;
        _ci7Init_audio_format = AUDIO_U16 ;
        _ci7Init_audio_channels = 2;
        _ci7Init_audio_buffers = 4096;
        break;
    case CI7_SOUND_HGH:
        _ci7Init_audio_rate = 44100;
        _ci7Init_audio_format = AUDIO_S16 ;
        _ci7Init_audio_channels = 2;
        _ci7Init_audio_buffers = 4096;
        break;
    default:
        _ci7Init_audio_rate = 32000;
        _ci7Init_audio_format = AUDIO_U16 ;
        _ci7Init_audio_channels = 2;
        _ci7Init_audio_buffers = 4096;
        break;
    }

    if(Mix_OpenAudio(_ci7Init_audio_rate, _ci7Init_audio_format, _ci7Init_audio_channels, _ci7Init_audio_buffers)) {
        printf("Unable to open audio!\n");
        exit(1);
    }

    Mix_VolumeMusic(128);
}

void CI7_Sound::SoundQuit ( void ) {
    Mix_CloseAudio();
}

void CI7_Sound::SoundloadFX ( const char * uri ) {
    sound = Mix_LoadWAV(uri);
    if(sound == NULL) {
        fprintf(stderr, "Unable to load WAV file: %s\n", Mix_GetError());
    }
}

void CI7_Sound::SoundplayFX ( void ) {
    int channel;

    channel = Mix_PlayChannel(-1, sound, 0);
    if(channel == -1) {
        fprintf(stderr, "Unable to play WAV file: %s\n", Mix_GetError());
    }
}

void CI7_Sound::SoundVolumeFX ( int iVolume ) {
    Mix_Volume(-1, iVolume);
}
void CI7_Sound::SoundUnloadFX(void) {
    Mix_FreeChunk( sound );
}

void  CI7_Sound::SoundloadSONG(  const char * uri ) {
    music = Mix_LoadMUS(uri);
}

void  CI7_Sound::SoundPlaySONG( void  ) {
    Mix_PlayMusic(music, 0);
}

void  CI7_Sound::SoundStopSONG( void ) {
    Mix_HaltMusic();
}

void  CI7_Sound::SoundPauseSONG(  void ) {
    Mix_Pause(-1);
}

void CI7_Sound::SoundVolumeSONG ( int iVolume ) {
    Mix_VolumeMusic(iVolume);
}

void  CI7_Sound::SoundUnLoadSONG( void  ) {
    Mix_FreeMusic ( music);
}
